package com.mango.banner.widget;

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * @author dengjiajin
 * @date 2018-10-22
 */
public class BannerAdapter extends PagerAdapter {

  private List<View> mImageViews;
  private BannerListener mListener;

  public BannerAdapter(List<View> imageViews, BannerListener listener) {
    mImageViews = imageViews;
    mListener = listener;
  }

  @Override
  public int getCount() {
    return mImageViews == null ? 0 : mImageViews.size();
  }

  @Override
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == object;
  }

  @NonNull
  @Override
  public Object instantiateItem(@NonNull ViewGroup container, final int position) {
    View view = mImageViews.get(position);
    container.addView(view);
    if (mListener != null) {
      view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          mListener.onBannerClick(position);
        }
      });
    }
    return view;
  }

  @Override
  public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((View) object);
  }
}

